import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Main from '../components/Main.vue'
import Welcome from '../components/Welcome.vue'
import Iplist from '../components/ip/Iplist.vue'
import Segment from '../components/ip/Segment.vue'
import Userlist from '../components/org/Userlist.vue'
import Department from '../components/org/Department.vue'
import Equiplist from '../components/assets/Equiplist.vue'
import Models from '../components/assets/Models.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/main',
    component: Main,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/ip/ipList', component: Iplist },
      { path: '/ip/segment', component: Segment },
      { path: '/org/userlist', component: Userlist },
      { path: '/org/department', component: Department },
      { path: '/assets/equiplist', component: Equiplist },
      { path: '/assets/models', component: Models }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const token = sessionStorage.getItem('token')
  if (!token) return next('/login')
  next()
})

export default router
